/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package muhnes.sizoo.dal;

import java.util.List;
import javax.annotation.PreDestroy;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import muhnes.sizoo.modelo.Orden;
import muhnes.sizoo.HibernateUtilZoologia;
/**
 *
 * @author Ricardo Orellana
 */
public class OrdenDaoImpl implements OrdenDao{
    
    Session sesion;
    
    public OrdenDaoImpl() {
        sesion = HibernateUtilZoologia.getSessionFactory().openSession();
    }
    
    @PreDestroy
    public void destroy() {
        sesion.close();
    }
    
    @Override
    public void guardar( Orden orden ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.save( orden );
        transaccion.commit();
    }

    @Override
    public void guardarColeccion( List< Orden > ordenes ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.saveOrUpdate( ordenes );
        transaccion.commit();
    }

    @Override
    public void actualizar( Orden orden ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.update( orden );
        transaccion.commit();
    }

    @Override
    public void actualizarColeccion( List< Orden > ordenes ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.update( ordenes );
        transaccion.commit();
    }

    @Override
    public void eliminar( Orden orden ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.delete( orden );
        transaccion.commit();
    }

    @Override
    public void eliminarColeccion( List< Orden > ordenes ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.delete( ordenes );
        transaccion.commit();
    }

    @Override
    public void eliminarPorId( int id ) {
        Transaction transaccion = sesion.beginTransaction();
        Orden orden = buscarPorId( id );
        sesion.delete( orden );
        transaccion.commit();
    }

    @Override
    public Orden buscarPorId( int id ) {
        Transaction transaccion = sesion.beginTransaction();
        Query consulta = sesion.createQuery("from Orden where CodigoOrden = " + id);
        Orden orden = (Orden)consulta.uniqueResult();
        transaccion.commit();
        return orden;
    }

    @Override
    public List< Orden > buscarTodos() {
        Transaction transaccion = sesion.beginTransaction();
        Query consulta = sesion.createQuery("from Orden");
        List< Orden > ordenes = consulta.list();
        transaccion.commit();
        return ordenes;
    }
    
}
